package com.example.hotdog.task;


import com.example.hotdog.HelloController;
import com.example.hotdog.entity.Renwu;
import com.example.hotdog.httpu.HttpReHot;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class HotTask {
    public static ConcurrentMap taskMap = new ConcurrentHashMap();


    public static void addTask(Renwu renwu){

            startTask(renwu);


    }

    public static void cancelTask(String taskName){
        taskMap.remove(taskName);

    }

    public static void startTask(Renwu renwu){
        if (taskMap.get(renwu.getT1col2() + renwu.getT1col3()) == null){
            taskMap.put(renwu.getT1col2() + renwu.getT1col3(),true);

            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true){
                        boolean equals = Objects.equals(true, taskMap.get(renwu.getT1col2() + renwu.getT1col3()));
                        if (!equals){
                            break;
                        }

                        for (Map.Entry<String, Boolean> entry :  HelloController.amountMap.entrySet()) {
                            //java中For-Each循环中逐渐把“：”后的内容放到冒号前中。


                            String token = entry.getKey();
                            try {
                                String[] split = renwu.getT1col3().split("&");

                                List<Map> mapList = HttpReHot.postQ(token,split[1],split[0],renwu.getT1col4());
                                if (mapList.isEmpty()){
                                    continue;
                                }
                                int shu = Integer.parseInt(HelloController.userMap.get("suoNum").toString());

                                String cookie = null;

                                for (int j = 0; j < mapList.size(); j++) {
                                    if (j < shu){
                                        String secondId = mapList.get(j).get("second_id").toString();

                                        if (cookie == null){
                                            cookie = HttpReHot.postSave1(renwu.getToken());
                                        }
                                        String orderId = mapList.get(j).get("order_id").toString();
                                        if (cookie != null){
                                            HttpReHot.postSave2(renwu.getToken(),cookie,secondId,orderId);
                                        }else {
                                            break;
                                        }
                                    }
                                    break;
                                }
                                break;

                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException(e);
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }

                        }

                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }

                    }

                }
            });
            //开启线程
            thread.start();

        }



    }
}
